<?php
    class NewsController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        
        public function init() {
            $this->loadTemplates();
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Default_Model_News();
        }
        
        public function indexAction() {
            $this->view->headTitle('Tournow - Tin tức');
            $listNews = $this->_model->listNews();
            $adapter = new Zend_Paginator_Adapter_DbSelect($listNews);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
        }
        
        public function detailsAction() {
            $this->view->headTitle('Tournow - Tin tức chi tiết');
            $news_id =$this->_request->getParam("id");
            $list= $this->_model->NewsDetails($news_id);
            $news_id        =$list[0]["news_id"];
            $news_title     =$list[0]["news_title"];
            $news_image     =$list[0]["news_image"];
            $news_content   =$list[0]["news_content"];
            $news_plaintext =$list[0]["news_plaintext"];
            $date = Zend_Date::now();
            $day=$date->get(Zend_Date::WEEKDAY_DIGIT);
            $ord_date = date('d/m/Y, H:m').' (GMT+7)';
            $this   ->view->currday=$day;
            $this   ->view->currdate=$ord_date;
            $this   ->view->news_id=$news_id;
            $this   ->view->news_title=$news_title;
            $this   ->view->news_image=$news_image;
            $this   ->view->news_content=$news_content;
            $this   ->view->news_plaintext=$news_plaintext;
            $listNews = $this->_model->listNewsLimit();
            $this->view->data=$listNews;
            $listscm= $this->_model->listcomment($news_id);
            $this->view->listcm=$listscm;
            $auth = Zend_Auth::getInstance();
            $infoUser = $auth->getIdentity();
            $user_id = $infoUser->user_id;
            $this->view->userinfor=$user_id ;
            if($this->_request->isPost()){
                $content = $this->_request->getPost("content");
                $filter = new Zend_Filter_StringTrim();
                $content = $filter->filter($content);
                $news_id= $news_id;
                $date = date('Y-m-d');
                $status = 1;
                $user_id =$user_id;
                if($content==""){
                    $err="Bạn chưa nhập lời bình!";
                    $this->view->errorcontent=$err;
                }
                else{
                     $date=array('user_id'=>$user_id
                        ,'news_id'=>$news_id
                        ,'news_comment_content'=>$content
                        ,'create_date'=>$date
                        ,'status'=>$status);
                    $result = $this->_model->addNewsComment($date);
                    $this->_redirect('news/details/id/'.$news_id);
                    
                }
               
            }
            
     
        } 
    }



?>
